package com.geovis.emergency.spd.biz.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.common.auth.util.AuthUtil;
import com.geovis.emergency.spd.biz.system.mapper.SystemMainRoleMapper;
import com.geovis.emergency.spd.biz.system.mapper.SystemRoleMapper;
import com.geovis.emergency.spd.biz.system.mapper.SystemUserRoleMapper;
import com.geovis.emergency.spd.biz.system.service.ISystemUserRoleService;
import com.geovis.emergency.spd.entity.system.entity.SystemMainRole;
import com.geovis.emergency.spd.entity.system.entity.SystemRole;
import com.geovis.emergency.spd.entity.system.entity.SystemUserRole;
import com.geovis.emergency.spd.entity.system.pojo.enums.RoleTypeEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author 王响
 * @since 2020-07-15
 */
@Service
@AllArgsConstructor
@Slf4j
public class SystemUserRoleServiceImpl extends ServiceImpl<SystemUserRoleMapper, SystemUserRole> implements ISystemUserRoleService {

    private final SystemRoleMapper systemRoleMapper;
    private final SystemMainRoleMapper systemMainRoleMapper;

    @Override
    public List<String> selectNoRoleUserId() {
        return baseMapper.selectNoRoleUserId();
    }

    @Override
    public Boolean isSuperAdmin() {
        LambdaQueryWrapper<SystemRole> srWrapper = Wrappers.lambdaQuery(SystemRole.class)
                .eq(SystemRole::getEnName, "admin")
                .eq(SystemRole::getRoleType, RoleTypeEnum.SYSTEM);
        SystemRole role=systemRoleMapper.selectOne(srWrapper);
        String userId=AuthUtil.getCurrentUserId();
        LambdaQueryWrapper<SystemUserRole> wrapper = Wrappers.lambdaQuery(SystemUserRole.class)
                .eq(SystemUserRole::getUserId,userId)
                .eq(SystemUserRole::getRoleId,role.getId());
        Long count=baseMapper.selectCount(wrapper);
        return count>0;
    }
}
